#ifndef OTA_THREADPOOL_H_CAESAR__DEF
#define OTA_THREADPOOL_H_CAESAR__DEF

#include <windows.h>

class CWorkDesc
{
public:
	CWorkDesc(){}

	virtual ~CWorkDesc(){}
};

class CWork 
{
public:
	CWork(){}
	virtual	~CWork(){}
	virtual void ProcessJob(CWorkDesc* pJob)=0;
};


class CThreadPool
{
	friend	static	unsigned __stdcall ManagerProc(void* pThread);
	friend static	unsigned __stdcall WorkerProc(void* pThread);

	typedef struct  THREADINFO
	{
		THREADINFO()
		{
			hThreadHandle	=	NULL;
			dwThreadID		=	0;
			bIsBusy			=	false;
			bIsQuit			=	false;
		}
		HANDLE		hThreadHandle;
		unsigned	dwThreadID;
		volatile bool		bIsBusy;
		volatile bool		bIsQuit;
	}*LPTHREADINFO;
	
public:
	CThreadPool();
	virtual ~CThreadPool();
	

	enum	EReturnValue
	{
		MANAGERPROC_RETURN_VALUE	=	10001,
		WORKERPROC_RETURN_VALUE		=	10002
	};

	enum	EThreadStatus
	{
		BUSY,
		NORMAL,
		IDLE
	};

	bool Start(WORD	wStatic,WORD wMax);
	void Stop(void);
	void ProcessJob(CWorkDesc* pJob, CWork* pWorker) const;
		
protected:

	static	unsigned __stdcall ManagerProc(void* pThread);
	static	unsigned __stdcall WorkerProc(void* pThread);
	
	LPTHREADINFO		m_pThreadInfo;

	volatile WORD		m_wStaticThreadNum;
	volatile WORD		m_wMaxThreadNum;
	volatile bool		m_bQuitManager;
	DWORD				m_dwMSeconds;

	HANDLE				m_hManagerIO;
	HANDLE				m_hWorkerIO;

	HANDLE				m_hManagerThread;
	CRITICAL_SECTION	m_csLock;
private:
	CThreadPool::EThreadStatus	GetWorkThreadStatus();
	void	AddThread(void);
	void	DelThread(void);
	int		GetThreadbyID(DWORD dwID);
};
#endif





















